//
//  BaseQuestionStrategy.swift
//  RabbleWabble
//
//  Created by TRS-Mobile on 2021/9/9.
//

import Foundation

class BaseQuestionStrategy: QuestionStrategy {
    
    private var questionGroup: QuestionGroup {
        return questionGroupCaretaker.selectedQuestionGroup
    }
    private var questionIndex: Int = 0
    private var questions: [Question] = []
    private var questionGroupCaretaker: QuestionGroupCaretaker
    
    init(caretaker: QuestionGroupCaretaker, questions: [Question]) {
        self.questionGroupCaretaker = caretaker
        self.questions = questions
        self.questionGroupCaretaker.selectedQuestionGroup.score.reset()
    }
    
    var title: String {
        return questionGroup.title
    }
    
    var correctCount: Int {
        get {
            return questionGroup.score.correctCount
        }
        set {
            questionGroup.score.correctCount = newValue
        }
    }
    
    var incorrectCount: Int {
        get {
            return questionGroup.score.incorrectCount
        }
        set {
            return questionGroup.score.incorrectCount = newValue
        }
    }
    
    func advanceToNextQuestion() -> Bool {
        questionIndex += 1
        guard questionIndex < questionGroup.questions.count else {
            return false
        }
        try? questionGroupCaretaker.save()
        return true
    }
    
    func currentQuestion() -> Question {
        return questionGroup.questions[questionIndex]
    }
    
    func makeQuestionCorrect(_ question: Question) {
        correctCount += 1
    }
    
    func makeQuestionIncorrect(_ question: Question) {
        incorrectCount += 1
    }
    
    func questionIndexTitle() -> String {
        return "\(questionIndex + 1)/" + "\(questionGroup.questions.count)"
    }
    
}

